﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Web.UI;
using BLL;

namespace Web.admin.framePage
{
    public partial class left : ManagePage
    {
        private ModuleService moduleService = new ModuleService();
        private TeacherService teacherService = new TeacherService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lb_navigation_Bind();
                makeTree();
            }
        }

        protected void makeTree()
        {
            if (HttpContext.Current.User != null &&
                HttpContext.Current.User.Identity.IsAuthenticated &&
                HttpContext.Current.User is SitePrincipal)
            {
                SitePrincipal user = HttpContext.Current.User as SitePrincipal;
                SiteIdentity si = user.Identity as SiteIdentity;
                if (si.UserType != 0)
                    return;

                //ModuleVo rootModule = moduleService.getAllModules();
                ModuleVo rootModule = moduleService.getTeacherModules(si.UserID);
                TreeView1.Nodes.Clear();

                TreeNode rootNode = MakeTreeNode(rootModule);
                TreeView1.Nodes.Add(rootNode);
            }
        }

        protected TreeNode MakeTreeNode(ModuleVo moduleVo)
        {
            TreeNode node = new TreeNode();
            node.Text = moduleVo.DisplayName;
            node.Value = moduleVo.ModuleID;
            node.NavigateUrl = moduleVo.ModuleURL;
            node.Target = "mainFrame";
            foreach (ModuleVo child in moduleVo.Children)
            {
                TreeNode childNode = MakeTreeNode(child);
                node.ChildNodes.Add(childNode);
            }
            return node;
        }

        protected void lb_navigation_Bind()
        {
            StringBuilder stb = new StringBuilder();
            stb.Append("<script type=text/javascript> var tree = null;");
            stb.Append("var root = new TreeNode('系统菜单');");
            string permissionStr = string.Empty;
            if(Session["AdminPermission"]!=null&&Session["AdminPermission"].ToString()!="")
                permissionStr = new DbCommon.DbHelper().GetSingleItem("UserClass", "UserPermission", "UserId=" + Session["AdminPermission"].ToString()).ToString();
            string[] strArry = DbCommon.Utils.SplitString(permissionStr, ',');
            foreach (string str in strArry) {
                if (str == "1") {
                    stb.Append("var fun1 = new TreeNode('信息管理');root.add(fun1);");
                    foreach (string str0 in strArry)
                    {
                        if (str0 == "2")
                        {
                            stb.Append("var fun2 = new TreeNode('信息内容');fun1.add(fun2);");
                            foreach (string str1 in strArry) {
                                if (str1 == "3") {
                                    stb.Append("var fun3 = new TreeNode('信息列表', '../news/list.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun2.add(fun3);");
                                }
                                if (str1 == "4") {
                                    stb.Append("var fun4 = new TreeNode('信息编辑', '../news/edit.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun2.add(fun4);");
                                }
                            }
                        }
                        if (str0 == "5")
                        {
                            stb.Append("var fun7 = new TreeNode('信息栏目');fun1.add(fun7);");
                            foreach (string str2 in strArry) {
                                if(str2=="6")
                                stb.Append("var fun8 = new TreeNode('栏目列表', '../newsClass/List.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun7.add(fun8);");
                                if(str2=="7")
                                stb.Append("var fun9 = new TreeNode('栏目添加', '../newsClass/AddSingle.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun7.add(fun9);");
                            }
                        }
                    }
                }
                if (str == "8") {
                    stb.Append("var fun12 = new TreeNode('竞赛管理');root.add(fun12);");
                    foreach (string str3 in strArry)
                    {
                        if (str3 == "9")
                        {
                            stb.Append("var fun13 = new TreeNode('内容管理');fun12.add(fun13);");
                            foreach (string str4 in strArry) {
                                if (str4 == "10") 
                                    stb.Append("var fun14 = new TreeNode('竞赛列表', '../contest/List.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun13.add(fun14);");
                                if(str4=="11")
                                    stb.Append("var fun15 = new TreeNode('报名列表', '../contest/Apply.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun13.add(fun15);");
                                if(str4=="12")
                                    stb.Append("var fun16 = new TreeNode('添加竞赛', '../contest/AddSingle.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun13.add(fun16);");
                            }
                        }
                        if (str3 == "13") {
                            stb.Append("var fun19 = new TreeNode('过程管理');fun12.add(fun19);");
                            foreach(string str5 in strArry){
                                if (str5 == "14")
                                    stb.Append("var fun22 = new TreeNode('优秀作品', '#', 'tree_node.gif', null, 'tree_node.gif', null);fun19.add(fun22);");
                                if(str5=="15")
                                    stb.Append("var fun23 = new TreeNode('获奖学生', '#', 'tree_node.gif', null, 'tree_node.gif', null);fun19.add(fun23);");
                                if(str5=="16")
                                    stb.Append("var fun24 = new TreeNode('经费报销', '#', 'tree_node.gif', null, 'tree_node.gif', null);fun19.add(fun24);");
                                if(str5=="17")
                                    stb.Append("var fun25 = new TreeNode('添加过程', '#', 'tree_node.gif', null, 'tree_node.gif', null);fun19.add(fun25);");
                            }
                        }
                    }
                }
                if (str == "18") {
                    stb.Append("var fun32 = new TreeNode('学生管理');root.add(fun32);");
                    foreach (string str6 in strArry)
                    {
                        if(str6=="19")
                            stb.Append("var fun33 = new TreeNode('批量导入', '../students/InsertAll.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun32.add(fun33);");
                        if(str6=="20")
                            stb.Append("var fun35 = new TreeNode('学生列表', '../students/List.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun32.add(fun35);");
                    }
                }
                if (str == "21") {
                    stb.Append("var fun38 = new TreeNode('用户管理');root.add(fun38);");
                    foreach (string str7 in strArry)
                    {
                        if(str7=="22")
                            stb.Append("var fun39 = new TreeNode('用户列表', '../User/List.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun38.add(fun39);");
                        if(str7=="23")
                            stb.Append("var fun40 = new TreeNode('添加用户', '../User/AddSingle.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun38.add(fun40);");
                    }
                }
                if (str == "24") {
                    stb.Append("var fun43 = new TreeNode('系统管理');root.add(fun43);");
                    foreach (string str8 in strArry)
                    {
                        if (str8 == "25")
                            stb.Append("var fun44 = new TreeNode('操作日志', '../web/WebLog.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun43.add(fun44);");
                        if (str8 == "26")
                            stb.Append("var fun45 = new TreeNode('权限管理', '../web/Permission.aspx', 'tree_node.gif', null, 'tree_node.gif', null);fun43.add(fun45);");
                    }
                }
            }
            stb.Append("tree = new Tree(root); tree.show('menuTree');</script>");
            lb_navigation.InnerHtml = stb.ToString();
        }
    }
}